package com.amkai.aes.webservice.mobile;

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.Properties;

import com.amkai.aes.aescommon.ServerSettings;
import com.amkai.common.base.aoconfig.schema.AOConfigManager;

/**
 * 
 * @author lkovari
 *
 */
public class MobileServerConfigManager {
	public static final String DEFAULT_MOBILE_SERVER_CONFIG_NAME = "mobileserver.properties";
	public static Properties mobileConfigurationProperties = null;
	
	/**
	 * 
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static Properties loadPropertyFile(String fileName) throws Exception {
        Properties props = new Properties();
        URL url = ClassLoader.getSystemResource(fileName);
        if (url != null)
        	props.load(url.openStream());
        else {
        	FileInputStream propFile = new FileInputStream(fileName);
        	if (propFile != null)
        		props.load(propFile);
        }
        return props;
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Properties loadConfigurationFile() throws Exception {
		Properties props = null;
		String catalinaHome = ServerSettings.getCatalinaHome();
		String configFilePath = catalinaHome + AOConfigManager.AMKAI_OFFICE_CONFIG_PATH + DEFAULT_MOBILE_SERVER_CONFIG_NAME;
		props = loadPropertyFile(configFilePath);
		if (props != null)
			mobileConfigurationProperties = props;
		return props;
	}
	
	/**
	 * 
	 * @return
	 */
	public static boolean isMobileConfigurationLoaded() {
		return mobileConfigurationProperties != null;
	}
	
	/**
	 * 
	 * @param tag
	 * @return
	 */
	public static Boolean isProperty(String tag) {
		String propertyValue = null;
		Boolean isTrue = null;
		if ((mobileConfigurationProperties != null) && (mobileConfigurationProperties.containsKey(tag))) {
			propertyValue = mobileConfigurationProperties.getProperty(tag);
			if (propertyValue != null) {
				isTrue =  Boolean.parseBoolean(propertyValue);
			}
		}
		return isTrue;
	}
	
	/**
	 * 
	 * @return
	 */
	public static Boolean IsShowPatientName() {
		return isProperty(MobileServerConfigTags.SHOW_PERSON_OR_PATIENT_NAME);
	}
}
